<?php

namespace App\Models\GameDB\WholeGame;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CheckUser extends Model
{
    public $connection = 'mysql_game';

    protected $table = 'whole_game_check_user';

    protected $fillable = [
        'user_id',
        'input',
        'output',
        'input_date',
        'output_date',
        'input_week',
        'output_week',
    ];

    public function user()
    {
        return $this->hasOne(\App\Models\MainDB\User::class, 'user_id', 'user_id');
    }

    public function user_data()
    {
        return $this->hasOne(\App\Models\MainDB\UserData::class, 'user_id', 'user_id');
    }

    public function firstOrCreate($userId)
    {
        return self::query()->firstOrCreate(['user_id' => $userId], ['user_id' => $userId]);
    }

    public function userUpdateData($userId, $data)
    {
        $this->firstOrCreate($userId);

        $input  = $data['input'];
        $output = $data['output'];
        $this->where('user_id', $userId)->update([
            'input' => DB::raw('input +' . $input),
            'output' => DB::raw('output +' . $output),
            'input_date' => DB::raw('input_date +' . $input),
            'output_date' => DB::raw('output_date +' . $output),
            'input_week' => DB::raw('input_week +' . $input),
            'output_week' => DB::raw('output_week +' . $output),
        ]);
    }

}
